class pay{
	//总订单支付
	async orderPay(pay_type,data){
		if(pay_type==="wxpay"){
			return await this.wxpay(data)
		}else if(pay_type==="alipay"){
			return await this.alipay(data)
		}else if(pay_type==="towxpay"){
			return await this.toWxPay(data)
		}else if(pay_type==="toalipay"){
            return await this.toAlipay(data)
		}else if(pay_type==="yue"){
			return await this.yuePayOrder(data)
		}
	}
	
	//余额支付订单
	yuePayOrder(post){
		return new Promise((resolve, reject) => {	
			let than=this;
			post.pay_type='yue';
			//调起支付
			uni.$u.api.orderPay(post).then(res =>{
				resolve(res)
			}).catch(res=>{
				reject(res)
			})
		})
	}
	
	// 微信支付 - 立即发起
	toWxPay(data){
		return new Promise((resolve, reject) => {	
			let than=this;
			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: data.timeStamp,
				nonceStr: data.nonceStr,
				package: data.package,
				signType: data.signType,
				paySign: data.paySign,
				success: function (res) {
					resolve(res)
				},
				fail: function (err) {
					if(err.errMsg=="requestPayment:fail cancel"){
						than.cancelPay();
						reject('cancel');
					}else{
						reject(err.errMsg);
					}
				}
			})
		});
	}
	
	//支付宝支付 - 立即发起
	toAlipay(res){
        return new Promise((resolve, reject) => {
            uni.requestPayment({
            	provider: 'alipay',
            	orderInfo: res.tradeNo,
            	success: function (payres) {
            		let data={
            			status:false,
            			code:payres.resultCode,
            			tips:""
            		}
            		switch(payres.resultCode){
            			case "4" :
            				data.tips="无权限调用"
            				break
            			case "9000" :
            				data.status=true
            				data.tips="订单处理成功"
            				break
            			case "8000" :
            				data.status=true
            				data.tips="正在处理中"
            				break
            			case "4000" :
            				data.tips="订单处理失败"
            				break
            			case "6001" :
            				data.tips="您已取消授权"
            				break
            			case "6002" :
            				data.tips="网络连接出错，请重试"
            				break
            			case "6004" :
            				data.tips="处理结果未知"
            				break
            		}
            		resolve(data)
            	},
            	fail: function (err) {
            		if(err.errMsg=="requestPayment:fail cancel"){
            			reject('fail')
            		}
            	}
            });
        });
    }
	
	//微信支付
	wxpay(post){
		return new Promise((resolve, reject) => {	
			let pay_type="wxpay"
			let than=this;
			post.pay_type=pay_type
			//调起支付
			uni.$u.api.orderPay(post).then(res =>{
				if(res.data.auto==1){			//是否0元订单
					resolve(res)
				}else{
					uni.requestPayment({
						provider: pay_type,
						timeStamp: res.data.timeStamp,
						nonceStr: res.data.nonceStr,
						package: res.data.package,
						signType: res.data.signType,
						paySign: res.data.paySign,
						success: function (res) {
							resolve(res)
						},
						fail: function (err) {
							if(err.errMsg=="requestPayment:fail cancel"){
								reject('fail')
							}
						}
					});
				}
			}).catch(e=>{
				reject(e)
			})
		})
	}
	
    
	//支付宝支付
	alipay(post){
		return new Promise((resolve, reject) => {	
			let pay_type="alipay"
			let than=this;
			post.pay_type=pay_type
			//调起支付
			uni.$u.api.orderPay(post).then(res =>{
				if(res.data.auto==1){			//是否0元订单
					resolve(res)
				}else{
					uni.requestPayment({
						provider: pay_type,
						orderInfo: res.data.tradeNo,
						success: function (payres) {
							let status=false;
							let code=payres.resultCode;
							let tips="";
							switch(code){
								case "4" :
									tips="无权限调用"
									break
								case "9000" :
									status=true
									tips="订单处理成功"
									break
								case "8000" :
									status=true
									tips="正在处理中"
									break
								case "4000" :
									tips="订单处理失败"
									break
								case "6001" :
									tips="您已取消授权"
									break
								case "6002" :
									tips="网络连接出错，请重试"
									break
								case "6004" :
									tips="处理结果未知"
									break
							}
							if(status){
								resolve(true)
							}else{
								reject(tips)
							}
							
						},
						fail: function (err) {
							if(err.errMsg=="requestPayment:fail cancel"){
								reject('fail')
							}
						}
					});
				}
			}).catch(e=>{
				console.log(e);
				reject(e)
			})
		})
	}
	
	//支付宝预授权
	aliAuthPay(str){
		return new Promise((resolve, reject) => {
			my.tradePay({
				orderStr: str,
				success: (res) => {
					let data={
						status:false,
						code:res.resultCode,
						tips:""
					}
					switch(res.resultCode){
						case "4" :
							data.tips="无权限调用"
							break
						case "9000" :
							data.status=true
							data.tips="订单处理成功"
							break
						case "8000" :
							data.status=true
							data.tips="正在处理中"
							break
						case "4000" :
							data.tips="订单处理失败"
							break
						case "6001" :
							data.tips="您已取消授权"
							break
						case "6002" :
							data.tips="网络连接出错，请重试"
							break
						case "6004" :
							data.tips="处理结果未知"
							break
					}
					resolve(data)
				},
				fail: (res) => {
					reject(JSON.stringify(res))
				}
			})
		})
	}
}
export default new pay();